package com.example.ui.controller;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RuntimeUtil;
import cn.hutool.core.util.StrUtil;
import com.example.ui.dto.Txt;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author yliu
 */
@RestController
@RequestMapping("/cmd")
@Slf4j
public class CmdController {


    @PostMapping("/submit")
    public ResponseEntity<Object> submit(@RequestBody Txt txt) throws Exception {
        if (StrUtil.isEmpty(txt.getContent())) {
            return ResponseEntity.ok("内容不能为空");
        }
        String workPath = "/home/admin/PaddleSpeech/examples/other/tts_finetune/tts3";
        String headCmd = "docker exec -i tts_train bash -c ";
        String endCmd = "\"cd " + workPath + " && echo '" + txt.getContent() + "' > sentences.txt && sh tts_file.sh\"";
        String allCmd = headCmd + endCmd;
        log.info("命令行：{}", allCmd);
        String[] cmd = {"/bin/sh", "-c", allCmd};
        String result = RuntimeUtil.execForStr(cmd);
        log.info("执行结果：{}", result);
        return ResponseEntity.ok(result);
    }

    @GetMapping("/min/{type}/{num}")
    public String min(@PathVariable("type") String type, @PathVariable("num") Integer num) {
        DateTime now = DateUtil.date();
        String beginTimeStr = "";
        String endTimeStr = now.toString();
        if ("day".equals(type)) {
            beginTimeStr = DateUtil.offset(now, DateField.DAY_OF_MONTH, -num).toString();
        } else if ("hour".equals(type)) {
            beginTimeStr = DateUtil.offset(now, DateField.HOUR_OF_DAY, -num).toString();
        } else {
            return "参数错误";
        }
        long start = System.currentTimeMillis();
        log.info("=====开始同步患者缴费记录=====开始时间:{}=====结束时间:{}", beginTimeStr, endTimeStr);
        log.info("=====结束同步患者缴费记录=====开始时间:{}=====结束时间:{}", beginTimeStr, endTimeStr);
        long end = System.currentTimeMillis();
        log.info("=====执行耗时:{}ms======", end - start);
        return "同步数据成功";
    }

}
